import type { FormItemRule } from 'element-plus'

/**
 * 手机号码验证
 * 用于表单验证
 */
export function validatorMobile(_: any, mobile: string, callback: (error?: string | Error) => void) {
    // 允许空值，若需必填请添加多验证规则
    if (!mobile) {
        return callback()
    }
    const reg = /^1[3-9]\d{9}$/;
    if (!reg.test(mobile)) {
        return callback(new Error("请输入正确的手机号码"))
    }
    return callback()
}


/**
 * 账户名验证
 */
export function validatorAccount(rule: any, val: string, callback: (error?: string | Error) => void) {
    if (!val) {
        return callback()
    }
    if (!/^[a-zA-Z][a-zA-Z0-9_]{2,15}$/.test(val)) {
        return callback(new Error("请输入正确的帐户"))
    }
    return callback()
}

/**
 * 密码验证
 */
export function regularPassword(val: string) {
    return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z][^\u4e00-\u9fa5]{6,20}$/.test(val);

}

export function validatorPassword(rule: any, val: string, callback: (error?: string | Error) => void) {
    if (!val) {
        return callback()
    }
    if (!regularPassword(val)) {
        return callback("密码强度不足,必须包含字母和数字")
    }
    return callback()
}

/**
 * 变量名验证
 */
export function validatorVarName(rule: any, val: string, callback: (error?: string | Error) => void) {
    if (!val) {
        return callback()
    }
    if (!/^([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*$/.test(val)) {
        return callback(new Error("请输入正确的名称"))
    }
    return callback()
}

// @ts-ignore

export function validatorEditorRequired(rule: any, val: string, callback: (error?: string | Error) => void) {
    if (!val || val == '<p><br></p>') {
        return callback(new Error("验证内容不能为空"))
    }
    return callback()
}

/**
 * 构建表单验证规则
 * @var ruleName 规则名:required=必填,mobile=手机号,account=账户,password=密码,varName=变量名,number、integer、float、date、url、email
 * @var title 验证项的标题
 * @var trigger 自定义验证触发方式
 * @var message 自定义验证错误消息
 */
export const validatorType = {
    required: "必填项",
    mobile: "手机号不正确",
    account: "账号不正确",
    password: "密码错误",
    varName: "变量错误",
    url: 'URL',
    email: "邮箱地址错误",
    date: "时间歌词不正确",
    number: "数字不正确",
    integer: "不是一个证书",
    float: "浮点数不正确",
}

export function buildValidatorData(ruleName: string, title: string = '', trigger: string = 'blur', message: string = ''): FormItemRule {
    // 必填
    if (ruleName == 'required') {
        return {
            required: true,
            message: message ? message : "必填项",
            trigger: trigger,
        }
    }
    // 常见类型
    let validatorType = ['number', 'integer', 'float', 'date', 'url', 'email']
    if (validatorType.includes(ruleName)) {
        return {
            type: ruleName as FormItemRule["type"],
            message: message ? message : "请输入正确的字段",
            trigger: trigger,
        }
    }
    // 自定义验证方法
    let validatorCustomFun: { [key: string]: FormItemRule["validator"] } = {
        mobile: validatorMobile,
        account: validatorAccount,
        password: validatorPassword,
        varName: validatorVarName,
        editorRequired: validatorEditorRequired
    }
    if (validatorCustomFun[ruleName]) {
        return {
            validator: validatorCustomFun[ruleName],
            trigger: trigger,
        }
    }
    return {}
}


/**
 * 验证必须勾选
 * @var rule 
 * @var val 
 * @var callback
 */
export const checkValidator=(_rule: any, val: boolean, callback: (error?: string | Error) => void)=>{
    if(val===true){
        callback(); 
        return;
    }
    callback("此项必须勾选");
};